package top.tangyh.lamp.care.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.tangyh.basic.annotation.log.WebLog;
import top.tangyh.basic.base.R;
import top.tangyh.lamp.care.entity.BCareContent;
import top.tangyh.lamp.care.entity.BCarePgVte;
import top.tangyh.lamp.care.entity.BHl7LisBar;
import top.tangyh.lamp.care.service.*;
import top.tangyh.lamp.care.vo.query.*;
import top.tangyh.lamp.care.vo.result.*;
import top.tangyh.lamp.care.vo.save.PgRecordVo;
import top.tangyh.lamp.common.utils.ConvertUtil;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: zqb
 * @date: 2024/8/7 14:43
 */
@Slf4j
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/doctor")
@Tag(name = "医生模块")
public class DoctorController {

    private final BObsvRecordService bObsvRecordService;
    private final BIoRecordService bIoRecordService;


    private final BPgRecordService bPgRecordService;

    private final BCarePgVteService bCarePgVteService;

    private final BWorkPlanService bWorkPlanService;

    private  final BHl7YzYaoExeService bHl7YzYaoExeService;

    private final BHl7YzService bHl7YzService;

    private final BCareContentService bCareContentService;

    private final ApachiiPickService apachiiPickService;

    @Operation(summary = "病情概览-查询医生用观察项记录")
    @GetMapping("/obsvRecord/query")
    public R<List<ObsvRecordDto>> queryByDoctor(@Valid ObsvRecordQueryByDoctorParam param) throws Exception {
        return R.success(bObsvRecordService.queryByDoctor(param));
    }

    /**
     * 查询出入量
     */
    @PostMapping(value = "/oivolume/list")
    @Operation(summary = "病情概览-查询出量和入量")
    public R<Map<String, Map<String, List<IoRecordVO>>>> statisticInAndOutSum(@RequestBody SelectStatisticParam param) {
        return R.success(bIoRecordService.listOfIoRecord(param));
    }

    @Operation(summary = "病情概览-评分记录")
    @PostMapping("/diseas/list")
    public R<DiseaseOverviewVO> getDiseaseOverview(@RequestBody DiseaseOverviewQueryVO queryVO) {

        //评估评分
        List<PgRecordVo> listRecord = bPgRecordService.getDiseaseOverviewPgRecord(queryVO.getHospitalNumber(), queryVO.getRkTime(), queryVO.getCkTime());
        DiseaseOverviewVO diseaseOverviewVO = new DiseaseOverviewVO();
        diseaseOverviewVO.setListRecod(listRecord);

        //vte
        List<CarePgVteDOVO> vteList = new ArrayList<>();
        for (int i = 0; i < queryVO.getTypes().size(); i++) {
            BCarePgVte vtePO = bCarePgVteService.getLastNewCarePgRecord(queryVO.getTypes().get(i), queryVO.getPatientIdent(), queryVO.getRkTime(), queryVO.getCkTime());
            if (null != vtePO) {
                CarePgVteDOVO vteVO = ConvertUtil.convert(vtePO, CarePgVteDOVO.class);
                vteList.add(vteVO);
            }

        }
        diseaseOverviewVO.setVteList(vteList);

        return R.success(diseaseOverviewVO);
    }

    @Operation(summary = "病情概览-查询排班时间轴", description = "查询排班时间轴")
    @WebLog("查询排班时间轴")
    @GetMapping("/work_plan/detail")
    public R<List<WorkPlanDetailOutVO>> queryDetail(WorkPlanDetailQueryVO queryVO) {
        List<WorkPlanDetailOutVO> resultList = bWorkPlanService.selectDetailListByQuery(queryVO);
        return R.success(resultList);
    }

    @Operation(summary ="病情概览-查询执行用药列表(护理记录里用到)")
    @PostMapping("/exe/yao/list")
    public R<List<ExeYaoOutVO>> selectListByQuery(@RequestBody ExeYaoQueryVO queryVO) {
        List<ExeYaoOutVO> result = bHl7YzYaoExeService.selectExeYaoListNoEventByQuery(queryVO);
        return R.success(result);
    }

    @Operation(summary = ("病情概览-检验申请 数据"))
    @GetMapping("/yz/bar/list")
    public R<List<BHl7LisBar>> selectYzBarListByQuery(ListBarQueryVO listBarQueryVO) {
        List<BHl7LisBar> result = bHl7YzService.selectLisBarByQuery(listBarQueryVO);
        return R.success(result);
    }

    @PostMapping("/queryCareContentByPidAndTime")
    @Operation(summary ="病程记录-根据时间、健康号查询护理记录列表")
    public R<List<BCareContentResultVO>> queryCareContentByPidAndTime(@RequestBody QueryCareContentByPidAndTimeParam queryCareContentByPidAndTimeParam) {
        if (queryCareContentByPidAndTimeParam == null || queryCareContentByPidAndTimeParam.getPatientId() == null || queryCareContentByPidAndTimeParam.getTime() == null) {
            return R.fail("参数错误");
        }
        List<BCareContentResultVO> list = bCareContentService.queryCareContentByPidAndTime(queryCareContentByPidAndTimeParam);

        return R.success(list);
    }

    @Operation(summary ="自动获取医生apachii评分所要数据")
    @GetMapping("/apacheii/data")
    public R<ApacheiiPickVO> apachiiPick(ApacheiiPickQueryInVO param){
        return R.success(apachiiPickService.apcciiPick(param));
    }

}
